//
// Copyright 2018 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// stylelint-disable selector-class-pattern --
// Selector '.mdc-*' should only be used in this project.

@use '@material/animation/variables' as animation-variables;
@use '@material/tab/mixins' as tab-mixins;
@use '@material/feature-targeting/feature-targeting';
@use './variables';

///
/// Sets the CSS transition for the tab scrolling animation.
///
/// @param {Number | String} $duration-ms - Duration (in ms) of the animation.
/// @param {String} $timing-function - Optionally overrides the default animation timing function.
///
@mixin transition(
  $duration-ms,
  $timing-function: animation-variables.$standard-curve-timing-function,
  $query: feature-targeting.all()
) {
  $feat-animation: feature-targeting.create-target($query, animation);

  &.mdc-tab-scroller--animating .mdc-tab-scroller__scroll-content {
    @include feature-targeting.targets($feat-animation) {
      transition: $duration-ms transform $timing-function;
    }
  }
}

@mixin static-styles($query: feature-targeting.all()) {
  @include core-styles($query: $query);
}

@mixin core-styles($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);
  $feat-animation: feature-targeting.create-target($query, animation);

  // postcss-bem-linter: define tab-scroller
  .mdc-tab-scroller {
    @include transition(
      $duration-ms: variables.$transition-duration,
      $query: $query
    );

    @include feature-targeting.targets($feat-structure) {
      overflow-y: hidden;
    }
  }

  // Selector for test element used to feature-detect horizontal scrollbar height
  .mdc-tab-scroller__test {
    @include feature-targeting.targets($feat-structure) {
      position: absolute;
      top: -9999px;
      width: 100px;
      height: 100px;
      overflow-x: scroll;
    }
  }

  .mdc-tab-scroller__scroll-area {
    @include feature-targeting.targets($feat-structure) {
      -webkit-overflow-scrolling: touch;
      display: flex;
      overflow-x: hidden;
    }
  }

  .mdc-tab-scroller__scroll-area,
  .mdc-tab-scroller__test {
    @include feature-targeting.targets($feat-structure) {
      &::-webkit-scrollbar {
        display: none;
      }
    }
  }

  // This modifier class will be added in JS after computing the OS scrollbar size in order to hide the scrollbar.
  .mdc-tab-scroller__scroll-area--scroll {
    @include feature-targeting.targets($feat-structure) {
      overflow-x: scroll;
    }
  }

  .mdc-tab-scroller__scroll-content {
    @include scroll-content_($query);
  }

  .mdc-tab-scroller--align-start .mdc-tab-scroller__scroll-content {
    @include feature-targeting.targets($feat-structure) {
      justify-content: flex-start;
    }
  }

  .mdc-tab-scroller--align-end .mdc-tab-scroller__scroll-content {
    @include feature-targeting.targets($feat-structure) {
      justify-content: flex-end;
    }
  }

  .mdc-tab-scroller--align-center .mdc-tab-scroller__scroll-content {
    @include feature-targeting.targets($feat-structure) {
      justify-content: center;
    }
  }

  .mdc-tab-scroller--animating {
    .mdc-tab-scroller__scroll-area {
      @include feature-targeting.targets($feat-animation) {
        -webkit-overflow-scrolling: auto;
      }
    }
  }

  // postcss-bem-linter: end
}

//
// Private
//

@mixin scroll-content_($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);
  $feat-animation: feature-targeting.create-target($query, animation);

  @include tab-mixins.parent-positioning($query);

  @include feature-targeting.targets($feat-structure) {
    display: flex;
    flex: 1 0 auto;
    transform: none;
  }

  @include feature-targeting.targets($feat-animation) {
    will-change: transform;
  }
}
